package cn.smilefox.dao.impl;

import cn.smilefox.dao.RoleDao;
import cn.smilefox.domain.Role;
import cn.smilefox.domain.Teacher;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class RoleDaoImpl implements RoleDao {

    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<Role> findall() {

        final List<Role> roleList = jdbcTemplate.query("select * from user_role", new BeanPropertyRowMapper<Role>(Role.class));
        System.out.println(roleList);
        return roleList;
    }

    @Override
    public void save(Role role) {
        jdbcTemplate.update("insert into user_role values(?,?,?)", null,role.getRolename(),role.getRoledesc());
    }

    @Override
    public List<Role> findRoleByUserId(Long id) {
        List<Role> roles = jdbcTemplate.query("SELECT * FROM rela_teacher_role tr,user_role r WHERE tr.role_id=r.id AND tr.teacher_id=?;", new BeanPropertyRowMapper<Role>(Role.class), id);

        return roles;


    }
}
